package polep.wespace.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import polep.wespace.model.WeChatPublicNumber;
import polep.wespace.service.WeChatPublicNumberService;
import polep.wespace.utils.HttpClientUtil;
import polep.wespace.utils.Utils;

@Controller
@RequestMapping(value="/menu")
public class WxMenuController {
	@Resource(name="wechatPublicNumberService")
	private WeChatPublicNumberService WeChatPublicNumberService;
	@RequestMapping(value="/create")
	public  String createMenu(){
		String url = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=ACCESS_TOKEN";
		WeChatPublicNumber weChatPublicNumber = WeChatPublicNumberService.getFirstWeChatPublicNumber();
		String urln = url.replace("ACCESS_TOKEN", weChatPublicNumber.getAccessToken());
		System.out.println(urln);
		Map root = new HashMap();
		List<Map> buttonList = new ArrayList<Map>();
		Map button1 = new HashMap();
		button1.put("type", "click");
		button1.put("name", "开心一刻");
		button1.put("key", "V1001_HAPPY_ONECE");
		buttonList.add(button1);
		
		Map button2 = new HashMap();
		button2.put("type", "click");
		button2.put("name", "开心一刻1");
		button2.put("key", "V1001_HAPPY_ONECE1");
		buttonList.add(button2);
		
		Map button3 = new HashMap();
		button3.put("type", "click");
		button3.put("name", "开心一刻2");
		button3.put("key", "V1001_HAPPY_ONECE2");
		buttonList.add(button3);
		
		root.put("button", buttonList);
		
		String jsonStr = Utils.map2JsonStr(root);
		String respStr = HttpClientUtil.doPost(urln, jsonStr);
		System.out.println(jsonStr);
		
		return respStr;
	}
	
	public static void main(String[] args) {
		new WxMenuController().createMenu();
	}
}
